package work.konbai.education.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.tool.utils.ObjectUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import work.konbai.basic.vo.MemberVO;
import work.konbai.education.dto.SignInDTO;
import work.konbai.education.entity.AttendanceDetail;
import work.konbai.education.mapper.AttendanceDetailMapper;
import work.konbai.education.service.IAttendanceDetailService;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 考勤详情信息表 服务实现类
 *
 * @author KonBAI
 * @version 1.0
 * Create by 2021/3/3 16:22
 */
@Service
public class AttendanceDetailServiceImpl extends ServiceImpl<AttendanceDetailMapper, AttendanceDetail> implements IAttendanceDetailService {

	@Override
	public List<MemberVO> signDataList(Long courseId) {
		return baseMapper.selectSignDataList(courseId);
	}

	@Override
	public List<MemberVO> signedList(Long attendanceId) {
		return baseMapper.selectSignedList(attendanceId);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean signSubmit(SignInDTO signInDTO) {
		// 查询原有的学生集合
		List<AttendanceDetail> oldList = baseMapper.selectList(Condition.<AttendanceDetail>getLambdaQueryWrapper()
			.eq(AttendanceDetail::getAttendanceId, signInDTO.getAttendanceId()));
		// 筛选 原有学生ID不存在提交集合中的studentId
		List<AttendanceDetail> delStudentList = oldList.stream()
			.filter(item -> !signInDTO.getStudentIdList().contains(item.getStudentId())).collect(Collectors.toList());
		// 如果待删除列表不为空,则执行删除操作
		if (ObjectUtil.isNotEmpty(delStudentList)) {
			List<Long> delStudentIdList = delStudentList.stream().map(AttendanceDetail::getId).collect(Collectors.toList());
			if (!SqlHelper.retBool(baseMapper.deleteBatchIds(delStudentIdList))) {
				// 手动回滚事务
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
				return false;
			}
		}
		List<Long> oldStudentIdList = oldList.stream()
			.map(AttendanceDetail::getStudentId).collect(Collectors.toList());
		// 筛选 提交学生ID不存在原有集合中的studentId
		List<Long> addStudentIdList = signInDTO.getStudentIdList().stream()
			.filter(id -> !oldStudentIdList.contains(id)).collect(Collectors.toList());
		List<AttendanceDetail> attendanceDetailList = addStudentIdList.stream().map(studentId -> {
			AttendanceDetail detail = new AttendanceDetail();
			detail.setAttendanceId(signInDTO.getAttendanceId());
			detail.setStudentId(studentId);
			return detail;
		}).collect(Collectors.toList());
		if (ObjectUtil.isNotEmpty(attendanceDetailList)) {
			if (!SqlHelper.retBool(baseMapper.insertBatchSomeColumn(attendanceDetailList))) {
				// 手动回滚事务
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
				return false;
			}
		}
		return true;
	}
}
